package com.module.app.widget.keyboard;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.StringRes;
import androidx.fragment.app.FragmentActivity;

import com.module.app.widget.keyboard.base.KeyboardHeightFragment;
import com.module.app.R;
import com.module.app.widget.keyboard.base.KeyboardHeightFragment;
import com.module.frame.app.AppManager;


/**
 * @author bsnl_yanxuwen
 * @date 2020/9/29 10:27
 * Description :
 * 输入框弹窗
 */
public class InputConfirmDialog {
    private KeyboardHeightFragment fragment;
    private InputBuilder builder;

    public InputConfirmDialog(InputBuilder builder) {
        this.builder = builder;
    }

    public void hide() {
        if (fragment != null) {
            fragment.dismiss();
        }
    }

    public void show() {
        if (AppManager.getInstance().currentActivity() == null || !(AppManager.getInstance().currentActivity() instanceof FragmentActivity)) {
            return;
        }
        FragmentActivity fragmentActivity = (FragmentActivity) AppManager.getInstance().currentActivity();
        if (fragmentActivity == null || fragmentActivity.isFinishing()) {
            return;
        }
        fragment = KeyboardHeightFragment.newInstance(R.layout.app_dialog_input, R.id.editText, "", Color.parseColor("#99000000"));
        fragment.showAllowingStateLoss(fragmentActivity.getSupportFragmentManager(), "tag");
        fragment.setListener(new KeyboardHeightFragment.Listener() {

            @Override
            public void onDismiss() {
                if (builder != null && builder.mOnDismissListener != null) {
                    builder.mOnDismissListener.onDismiss(fragment.getDialog());
                }
            }

            @Override
            public void onComplete(View view) {
                fragment.getEditText().addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
                TextView tv_title = view.findViewById(R.id.tv_title);
                TextView tv_content = view.findViewById(R.id.tv_content);
                TextView tv_cancel = view.findViewById(R.id.tv_cancel);
                TextView tv_submit = view.findViewById(R.id.tv_submit);
                EditText editText1 = view.findViewById(R.id.editText);
                EditText editText2 = view.findViewById(R.id.editText2);
                View layout_edit2 = view.findViewById(R.id.layout_edit2);


                if (builder != null && !TextUtils.isEmpty(builder.title)) {
                    tv_title.setText(builder.title);
                }
                if (builder != null && !TextUtils.isEmpty(builder.content)) {
                    tv_content.setText(builder.content);
                    tv_content.setVisibility(View.VISIBLE);
                } else {
                    tv_content.setVisibility(View.GONE);

                }
                //1
                if (builder != null && !TextUtils.isEmpty(builder.editText)) {
                    editText1.setText(builder.editText);
                    editText1.setSelection(builder.editText.length());//将光标移至文字末尾
                }
                if (builder != null && !TextUtils.isEmpty(builder.hintText)) {
                    editText1.setHint(builder.hintText);
                }
                if (builder != null && builder.inputType != 0) {
                    editText1.setInputType(builder.inputType);
                }
                if (builder != null) {
                    editText1.setSingleLine(builder.singleLine);
                }
                if (builder != null && builder.maxLength > 0) {
                    InputFilter[] filters = {new InputFilter.LengthFilter(builder.maxLength)};
                    editText1.setFilters(filters);
                }
                //2
                if (builder != null && builder.isShowEdit2) {
                    layout_edit2.setVisibility(builder.isShowEdit2 ? View.VISIBLE : View.GONE);
                }
                if (builder != null && !TextUtils.isEmpty(builder.editText2)) {
                    editText2.setText(builder.editText2);
                    editText2.setSelection(builder.editText2.length());//将光标移至文字末尾
                }
                if (builder != null && !TextUtils.isEmpty(builder.hintText2)) {
                    editText2.setHint(builder.hintText2);
                }
                if (builder != null && builder.inputType2 != 0) {
                    editText2.setInputType(builder.inputType2);
                }
                if (builder != null) {
                    editText2.setSingleLine(builder.singleLine2);
                }
                if (builder != null && builder.maxLength2 > 0) {
                    InputFilter[] filters = {new InputFilter.LengthFilter(builder.maxLength2)};
                    editText2.setFilters(filters);
                }


                if (builder != null && !TextUtils.isEmpty(builder.cancelText)) {
                    tv_cancel.setText(builder.cancelText);
                    tv_cancel.setVisibility(View.VISIBLE);
                }
                if (builder != null && !TextUtils.isEmpty(builder.confirmText)) {
                    tv_submit.setText(builder.confirmText);
                }
                tv_submit.setOnClickListener(v -> {
                    if (builder != null && builder.mOnConfirmListener != null) {
                        builder.mOnConfirmListener.onClick(InputConfirmDialog.this, editText1.getText().toString());
                        builder.mOnConfirmListener.onClick(InputConfirmDialog.this, editText1.getText().toString(), editText2.getText().toString());

                    }
                });
                tv_cancel.setOnClickListener(v -> {
                    if (builder != null && builder.mOnCancelListener != null) {
                        builder.mOnCancelListener.onClick(v);
                    }
                    hide();
                });
                if (builder != null && !builder.mCanceledOnTouchOutside) {
                    fragment.setClickKeyboard(true);
                }
                if (builder != null && !builder.cancelable) {
                    fragment.setCancelable(false);
                }
                if (builder != null) {
                    tv_content.setGravity(builder.gravity);
                }
            }
        });

    }

    public static class Builder {
        private InputBuilder mController;

        public Builder(Context context) {
            mController = new InputBuilder();
            mController.context = context;

        }

        public Builder setTitle(@StringRes int titleId) {
            mController.title = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setTitle(CharSequence title) {
            mController.title = title;
            return this;
        }

        public Builder setContent(@StringRes int titleId) {
            mController.content = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setContent(CharSequence title) {
            mController.content = title;
            return this;
        }

        public Builder setEditText(@StringRes int text) {
            mController.editText = mController.context.getResources().getString(text);
            return this;
        }

        public Builder setEditText(CharSequence text) {
            mController.editText = text;
            return this;
        }

        public Builder setEditHint(@StringRes int text) {
            mController.hintText = mController.context.getResources().getString(text);
            return this;
        }

        public Builder setEditHint(CharSequence text) {
            mController.hintText = text;
            return this;
        }

        public Builder setInputType(int inputType) {
            mController.inputType = inputType;
            return this;
        }

        public Builder setSingleLine(boolean singleLine) {
            mController.singleLine = singleLine;
            return this;
        }

        public Builder setMaxLength(int maxLength) {
            mController.maxLength = maxLength;
            return this;
        }

        public Builder setEditText2(@StringRes int text) {
            mController.editText2 = mController.context.getResources().getString(text);
            return this;
        }

        public Builder setInputType2(int inputType) {
            mController.inputType2 = inputType;
            return this;
        }

        public Builder setEditText2(CharSequence text) {
            mController.editText2 = text;
            return this;
        }

        public Builder setEditHint2(@StringRes int text) {
            mController.hintText2 = mController.context.getResources().getString(text);
            return this;
        }

        public Builder setEditHint2(CharSequence text) {
            mController.hintText2 = text;
            return this;
        }

        public Builder setSingleLine2(boolean singleLine) {
            mController.singleLine2 = singleLine;
            return this;
        }

        public Builder setMaxLength2(int maxLength) {
            mController.maxLength2 = maxLength;
            return this;
        }

        public Builder showEdit2(boolean isShowEdit2) {
            mController.isShowEdit2 = isShowEdit2;
            return this;
        }

        public Builder setCancelButton(@StringRes int titleId) {
            mController.cancelText = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setCancelButton(CharSequence title) {
            mController.cancelText = title;
            return this;
        }

        public Builder setConfirmButton(@StringRes int titleId) {
            mController.confirmText = mController.context.getResources().getString(titleId);
            return this;
        }

        public Builder setConfirmButton(CharSequence title) {
            mController.confirmText = title;
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean mCanceledOnTouchOutside) {
            mController.mCanceledOnTouchOutside = mCanceledOnTouchOutside;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            mController.cancelable = cancelable;
            return this;
        }

        public Builder setOnCancelListener(View.OnClickListener onCancelListener) {
            mController.mOnCancelListener = onCancelListener;
            return this;
        }

        public Builder setOnConfirmListener(InputBuilder.OnClickListener mOnConfirmListener) {
            mController.mOnConfirmListener = mOnConfirmListener;
            return this;
        }

        /**
         * @see android.view.Gravity
         */
        public Builder setContentGravity(int gravity) {
            mController.gravity = gravity;
            return this;
        }

        public Builder setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
            mController.mOnDismissListener = onDismissListener;
            return this;
        }

        public InputConfirmDialog create() {
            return new InputConfirmDialog(mController);
        }

        public void show() {
            create().show();
        }
    }

}
